<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class  SingUp extends CI_Controller{
	
	function __construct(){
		parent::__construct();
		$this->load->library('form_validation');
		$this->load->helper('form');
		$this->form_validation->set_error_delimiters('<div class="help-block">', '</div>');
		$this->load->model('jugador');
	}
	
	function index(){
		if($this->session->userdata('logged_in')){
			redirect('home', 'refresh');
		}else{
			/* Setteo de reglas de validación */
			$this->form_validation->set_rules('nombre', 'Nombre', 'trim|required|xss_clean');
			$this->form_validation->set_rules('apellido','Apellido', 'trim|required|xss_clean');
			$this->form_validation->set_rules('username', 'Nombre de usuario', 'trim|required|xss_clean|is_unique[jugador.username]');
			$this->form_validation->set_rules('password', 'Contraseña', 'trim|required|xss_clean|matches[password-conf]');
			$this->form_validation->set_rules('descripcion', 'Descripción', 'trim|xss_clean');

			//* Corre el validador */
			$ok = $this->form_validation->run();
			/* Recuperacion de los datos */
			$data['jugador']['nombre'] = $this->input->post('nombre');
			$data['jugador']['apellido'] = $this->input->post('apellido');
			$data['jugador']['username'] = $this->input->post('username');
			$data['jugador']['password'] = md5($this->input->post('password'));
			$data['jugador']['descripcion'] = $this->input->post('descripcion');
			if($ok){
				$this->jugador->insert($data['jugador']);
				redirect('login');
			}else{
				$this->load->view('singup_view', $data);
			}
		}
	}
	function check_password($password){
		return ($password == $this->input->post('password-conf'));
	}	
}